﻿using System;
using System.Collections.Generic;
using System.Text;
using KC.Storage.BlobService;
using KC.Framework.Base;

namespace KC.Storage.Util.BlobHelper
{
    public partial class BlobProvider
    {
        public void CreateContainer(string containerName)
        {
            Provider.CreateContainer(containerName);
        }
        public void DeleteContainer(string containerName)
        {
            Provider.DeleteContainer(containerName);
        }

        public void SaveBlob(AuthToken token, BlobInfo blobObject, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            string containerName = token.ContainerName;
            SaveBlob(token, containerName, blobObject, isUserLevelBlob, expireAfterSecond);
        }
        public void SaveBlob(AuthToken token, string containerName, BlobInfo blobObject, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            SaveBlob(token, containerName, blobObject.Id, blobObject.FileType, blobObject.FileFormat, blobObject.FileName, blobObject.Data, isUserLevelBlob, expireAfterSecond);
        }
        public void SaveBlob(AuthToken token, string blobId, string type, string format, string fileName, byte[] fileBytes, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            SaveBlob(token, token.ContainerName, blobId, type, format, fileName, fileBytes, isUserLevelBlob, expireAfterSecond);
        }
        public void SaveBlob(AuthToken token, string containerName, string blobId, string type, string format, string fileName, byte[] fileBytes, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            string tenant = token.Tenant;
            string encryptionKey = token.EncryptionKey;
            string userId = token.Id;
            bool isInternal = token.Internal;
            Provider.SaveBlob(tenant, encryptionKey, isInternal, userId, containerName, blobId, type, format, fileName, fileBytes, isUserLevelBlob, expireAfterSecond);
        }
        public void SaveBlobWithoutEncryption(string containerName, BlobInfo blobObject)
        {
            Provider.SaveBlobWithoutEncryption(containerName, blobObject);
        }
        public void SaveBlobs(AuthToken token, List<BlobInfo> blobObjects, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            foreach (BlobInfo blobObject in blobObjects)
            {
                SaveBlob(token, blobObject.Id, blobObject.FileType, blobObject.FileFormat, blobObject.FileName, blobObject.Data, isUserLevelBlob, expireAfterSecond);
            }
        }

        public void AppendBlobMetadata(string containerName, string blobId, Dictionary<string, string> extraMetadata)
        {
            Provider.AppendBlobMetadata(containerName, blobId, extraMetadata);
        }

        public string SaveBlobBlock(AuthToken token, string blobId, byte[] blockBytes, string contentMD5 = null)
        {
            string blobBlockId = Convert.ToBase64String(Encoding.Default.GetBytes(Guid.NewGuid().ToString()));
            SaveBlobBlockWithId(token, blobId, blobBlockId, blockBytes, contentMD5);
            return blobBlockId;
        }
        public void SaveBlobBlockWithId(AuthToken token, string blobId, string blobBlockId, byte[] blockBytes, string contentMD5 = null)
        {
            string containerName = token.ContainerName;
            Provider.SaveBlobBlock(containerName, blobId, blobBlockId, blockBytes, contentMD5);
        }
        public void CommitBlobBlock(AuthToken token, string blobId, string type, string format,string fileName, long blobSize, string[] blockIds, bool isUserLevelBlob = false, long expireAfterSecond = -1)
        {
            string tenant = token.Tenant;
            string userId = token.Id;
            string containerName = token.ContainerName;
            bool isEncypted = false;
            bool isInternal = token.Internal;
            Dictionary<string, string> extraMetadata = null;
            Dictionary<string, string> metadata = BlobBase.BuildMetadata(tenant, isInternal, userId, blobId, type, format, fileName, blobSize, expireAfterSecond, isEncypted, isUserLevelBlob, extraMetadata);
            Provider.CommitBlobBlock(containerName, blobId, blockIds, metadata);
        }

        public void DeleteBlob(string containerName, params string[] blobId)
        {
            foreach (string oneBlobId in blobId)
            {
                Provider.DeleteBlob(containerName, oneBlobId);
                Provider.DeleteBlob(containerName, oneBlobId + ".metadata");
            }
        }
        public void DeleteBlobByRelativePath(string relativePath)
        {
            Provider.DeleteBlobByRelativePath(relativePath);
        }
        public void DeleteBlob(AuthToken token, params string[] blobId)
        {
            if (null != token && !string.IsNullOrEmpty(token.ContainerName))
            {
                DeleteBlob(token.ContainerName, blobId);
            }
        }

        public void CopyBlob(string containerName, string desContainerName, string blobId)
        {
            Provider.CopyBlob(containerName, desContainerName, blobId);
        }

        public void CopyBlobs(string containerName, string desContainerName, params string[] blobId)
        {
            foreach (string oneBlobId in blobId)
            {
                Provider.CopyBlob(containerName, desContainerName, oneBlobId);
            }
        }
    }
}
